package com.changcheng.project.obuupdata.controller;

import java.util.List;

import com.changcheng.common.TextUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.changcheng.framework.aspectj.lang.annotation.Log;
import com.changcheng.framework.aspectj.lang.enums.BusinessType;
import com.changcheng.project.obuupdata.domain.AObufileList;
import com.changcheng.project.obuupdata.service.IAObufileListService;
import com.changcheng.framework.web.controller.BaseController;
import com.changcheng.framework.web.domain.AjaxResult;
import com.changcheng.common.utils.poi.ExcelUtil;
import com.changcheng.framework.web.page.TableDataInfo;

/**
 * OBU升级文件Controller
 *
 * @author dazhi
 * @date 2020-10-30
 */
@Controller
@RequestMapping("/obufile/obuupdata")
public class AObufileListController extends BaseController {
    private String prefix = "obufile/obuupdata";

    @Autowired
    private IAObufileListService aObufileListService;

    @RequiresPermissions("obufile:obuupdata:view")
    @GetMapping()
    public String obuupdata() {
        return prefix + "/obuupdata";
    }

    /**
     * 查询OBU升级文件列表
     */
    @RequiresPermissions("obufile:obuupdata:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AObufileList aObufileList) {
        startPage();
        List<AObufileList> list = aObufileListService.selectAObufileListList(aObufileList);
        return getDataTable(list);
    }

    /**
     * 导出OBU升级文件列表
     */
    @RequiresPermissions("obufile:obuupdata:export")
    @Log(title = "OBU升级文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AObufileList aObufileList) {
        List<AObufileList> list = aObufileListService.selectAObufileListList(aObufileList);
        ExcelUtil<AObufileList> util = new ExcelUtil<AObufileList>(AObufileList.class);
        return util.exportExcel(list, "obuupdata");
    }

    /**
     * 新增OBU升级文件
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    int zhi = 0;

    /**
     * 新增保存OBU升级文件
     */
    @RequiresPermissions("obufile:obuupdata:add")
    @Log(title = "OBU升级文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AObufileList aObufileList) {
        if (!TextUtils.isBlank(aObufileList.getObufileAddress()) &&
                !TextUtils.isBlank(aObufileList.getObufileFactory()) &&
                !TextUtils.isBlank(aObufileList.getObufileVersion()) &&
                !TextUtils.isBlank(aObufileList.getObufileName()) &&
                !TextUtils.isBlank(aObufileList.getTime())

        ) {
            zhi = aObufileListService.insertAObufileList(aObufileList);
            return toAjax(zhi);
        } else {
            zhi = -1;
            return toAjax2(zhi, "数据不能为空！");
        }

    }

    /**
     * 修改OBU升级文件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AObufileList aObufileList = aObufileListService.selectAObufileListById(id);
        mmap.put("aObufileList", aObufileList);
        return prefix + "/edit";
    }

    /**
     * 修改保存OBU升级文件
     */
    @RequiresPermissions("obufile:obuupdata:edit")
    @Log(title = "OBU升级文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AObufileList aObufileList) {
        System.out.println(aObufileList);
        if (!TextUtils.isBlank(aObufileList.getObufileAddress()) &&
                !TextUtils.isBlank(aObufileList.getObufileFactory()) &&
                !TextUtils.isBlank(aObufileList.getObufileVersion()) &&
                !TextUtils.isBlank(aObufileList.getObufileName()) &&
                !TextUtils.isBlank(aObufileList.getTime())

        ) {
            zhi = aObufileListService.updateAObufileList(aObufileList);
            return toAjax(zhi);
        } else {
            zhi = -1;
            return toAjax2(zhi, "数据不能为空！");
        }
    }

    /**
     * 删除OBU升级文件
     */
    @RequiresPermissions("obufile:obuupdata:remove")
    @Log(title = "OBU升级文件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aObufileListService.deleteAObufileListByIds(ids));
    }


    /**
     * 根据厂家查询OBU升级文件列表
     */

    @PostMapping("/factorylist")
    @ResponseBody
    public TableDataInfo factorylist(AObufileList aObufileList) {
        startPage();
        List<AObufileList> list = aObufileListService.selectAObufileListList(aObufileList);
        return getDataTable(list);
    }
}
